import { defineConfig, loadEnv, ConfigEnv, UserConfigExport } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import { createHtmlPlugin } from 'vite-plugin-html'

const getViteEnv = (mode: any, target: any) => {
  return loadEnv(mode, process.cwd())[target]
}

// // https://vite.dev/config/
// export default defineConfig({
//   plugins: [
//     vue(),
//     AutoImport({
//       imports: ["vue", "vue-router", "pinia"], //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入

//       resolvers: [
//         VantResolver(),
//       ],
//     }),
//     Components({
//       resolvers: [VantResolver()],
//     }),
//     createHtmlPlugin({
//       inject: {
//         data: {

//           title: getViteEnv(process.env.MODE, 'VITE_APP_TITLE_CONTENT') //根据不同命令,改变地址栏标题
//         }
//       }
//     }),
//     // createVitePlugins(process.env.NODE_ENV, process.env.NODE_ENV === 'production')
//   ],
//   css: {
//     preprocessorOptions: {
//       scss: {
//         // additionalData: `@import "@/styles/variables.scss";`,
//       },
//     },
//   },
//   resolve: {
//     alias: {
//       '@': fileURLToPath(new URL('./src', import.meta.url))
//     }
//   },
//   server: {
//     port: 5175,
//     host: true,
//     open: false,
//     proxy: {
//       '/api': {
//         // target: 'http://10.11.228.239:8080',
//         // target: 'http://10.11.228.239:8000',
//         // target: 'http://10.11.225.242:8080',
//         target: 'http://10.11.227.145:8080',
//         // target: 'http://cspdevapi.cib-biz.com',
//         changeOrigin: true
//       },
//     }
//   },

// })


export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  return {
    base: '/mobile/', // 设置基础路径
    // base: getViteEnv(mode, 'VITE_APP_ENV') === 'development' ? '' : '/mobile', // 设置基础路径
    plugins: [
      vue(),
      AutoImport({
        imports: ["vue", "vue-router", "pinia"], //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
        dts: true, // 生成类型声明文件

        resolvers: [VantResolver()]
      }),
      Components({
        resolvers: [VantResolver()]
      }),
      createHtmlPlugin({
        inject: {
          data: {
            title: getViteEnv(mode, 'VITE_APP_TITLE_CONTENT') //根据不同命令,改变地址栏标题
          }
        }
      }),
      // createVitePlugins(process.env.NODE_ENV, process.env.NODE_ENV === 'production')
    ],

    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/styles/variables.scss";',
        }
      },
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },

    server: {
      port: 5175,
      host: true,
      open: false,
      proxy: {
        '/api': {
          // target: 'http://10.11.228.239:8080',
          // target: 'http://10.11.228.239:8000',
          // target: 'http://10.11.225.242:8080',
          // target: 'http://10.11.227.130:8080',
          target: 'https://cspdevapi.cib-biz.com',
          changeOrigin: true
        },
      }
    },
  }
}